package com.example.pc.mycreateview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.pc.mycreateview.R;
import com.example.pc.mycreateview.listener.MyViewClick;

public class TopBarView extends RelativeLayout {

    private Button leftButton,rightButton;
    private TextView textView;
    private ImageView leftImage,rightImage;

    private static final int b1_id = 0x01;
    private static final int b2_id = 0x02;
    private static final int b3_id = 0x03;
    private static final int b4_id = 0x04;
    private static final int b5_id = 0x05;

    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    private int leftImageView;

    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private int rightImageView;

    private float titleTextSize;
    private int titleTextColor;
    private String title;

    private LayoutParams leftParams,rightParams,titleParams,leftImageParams,rightImageParams;

    private MyViewClick myViewClick;

    public void setMyViewClick(MyViewClick myViewClick) {
        this.myViewClick = myViewClick;
    }

    public TopBarView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.TopBarView);

        leftTextColor=typedArray.getColor(R.styleable.TopBarView_leftTextColor,0);
        leftBackground=typedArray.getDrawable(R.styleable.TopBarView_leftBackground);
        leftText=typedArray.getString(R.styleable.TopBarView_leftText);

        leftImageView=typedArray.getResourceId(R.styleable.TopBarView_leftImageView,0);

        rightTextColor=typedArray.getColor(R.styleable.TopBarView_rightTextColor,0);
        rightBackground=typedArray.getDrawable(R.styleable.TopBarView_rightBackground);
        rightText=typedArray.getString(R.styleable.TopBarView_rightText);

        rightImageView=typedArray.getResourceId(R.styleable.TopBarView_rightImageView,0);

        titleTextSize=typedArray.getDimension(R.styleable.TopBarView_titleTextSize,0);
        titleTextColor=typedArray.getColor(R.styleable.TopBarView_titleTextColor,0);
        title=typedArray.getString(R.styleable.TopBarView_title);

        typedArray.recycle();

        leftButton=new Button(context);
        leftImage=new ImageView(context);
        rightButton=new Button(context);
        rightImage=new ImageView(context);
        textView=new TextView(context);

        leftButton.setId(b1_id);
        leftImage.setId(b2_id);
        rightButton.setId(b3_id);
        rightImage.setId(b4_id);
        textView.setId(b5_id);

        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);

        leftImage.setBackgroundResource(leftImageView);

        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        rightImage.setBackgroundResource(rightImageView);

        textView.setTextColor(titleTextColor);
        textView.setTextSize(titleTextSize);
        textView.setText(title);
        textView.setGravity(Gravity.CENTER);

        leftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_LEFT,b2_id);
        leftParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        leftParams.setMargins(10,0,0,0);
        addView(leftButton,leftParams);

        leftImageParams=new LayoutParams(80, 80);
        leftImageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        leftImageParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        leftImageParams.setMargins(20,0,0,0);
        addView(leftImage,leftImageParams);

        rightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_RIGHT,b4_id);
        rightParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        rightParams.setMargins(0,0,10,0);
        addView(rightButton,rightParams);

        rightImageParams=new LayoutParams(80, 80);
        rightImageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        rightImageParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        rightImageParams.setMargins(0,0,20,0);
        addView(rightImage,rightImageParams);

        titleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(textView,titleParams);

        //左边文字点击事件
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                myViewClick.leftClick();
            }
        });

        //右边文字点击事件
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                myViewClick.rightClick();
            }
        });

        //中间文字点击事件
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                myViewClick.titleClick();
            }
        });

        //左边Image点击
        leftImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                myViewClick.leftImageClick();
            }
        });

        //右边Image点击
        rightImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                myViewClick.rightImageClick();
            }
        });
    }

    //左边文字显示隐藏
    public void setLeftVisibility(boolean tags){
        if (tags){
            leftButton.setVisibility(VISIBLE);
        }else {
            leftButton.setVisibility(GONE);
        }
    }

    //右边文字显示隐藏
    public void setRightVisibility(boolean tags){
        if (tags){
            rightButton.setVisibility(VISIBLE);
        }else {
            rightButton.setVisibility(GONE);
        }
    }

    //中间文字显示隐藏
    public void setTitleVisibility(boolean tags){
        if (tags){
            textView.setVisibility(VISIBLE);
        }else {
            textView.setVisibility(GONE);
        }
    }

    //左边image显示隐藏
    public void setLeftImageVisibility(boolean tags){
        if (tags){
            leftImage.setVisibility(VISIBLE);
        }else {
            leftImage.setVisibility(GONE);
        }
    }

    //右边image显示隐藏
    public void setRightImageVisibility(boolean tags){
        if (tags){
            rightImage.setVisibility(VISIBLE);
        }else {
            rightImage.setVisibility(GONE);
        }
    }

    //设置标题栏背景颜色
    public void setBackgroundTitle(int TestColor){
        setBackgroundColor(TestColor);
    }
}
